package com.monkeyboy.mongo_demo;

import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.monkeyboy.util.MongoDBAuthUtil;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 投影约束
 *
 * @Author Gavin
 * @date 2021.01.11 14:22
 */
public class ProjectDocument {
    public static void main(String[] args) {
        ProjectDocument projectDocument = new ProjectDocument();
        projectDocument.selectDocumentWithProjectArth();
    }

    /**
     * 聚合算术运算
     * db.java_coll.aggregate([{$match:{username:'test'}},{$unwind:'$tags'},{$project:{_id:0,username:1,new_age:{$add:['$age',1]}}}])
     */
    public void selectDocumentWithProjectArth() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //第一个{}
        Document username = new Document();
        username.put("username", "test");
        Document match = new Document();
        match.put("$match", username);
        //第二个{}
        Document unwind = new Document();
        unwind.put("$unwind", "$tags");

        //第三个{}
        Document newAge = new Document();
        newAge.put("$add", Arrays.asList("$age", 1));
        Document field = new Document();
        field.put("_id", 0);
        field.put("username", 1);
        field.put("new_age", newAge);

        Document project = new Document();
        project.put("$project", field);


        List<Document> list = new ArrayList<>();
        list.add(match);
        list.add(unwind);
        list.add(project);
        AggregateIterable<Document> documents = collection.aggregate(list);
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("new_age"));
        }
    }

    /**
     * $project中的字符串操作
     * db.java_coll.aggregate([{$match:{username:'test'}},{$unwind:'$tags'},{$project:{_id:0,Title_Tags:{$concat:["$username","-","$tags"]}}}])
     */
    public void selectDocumentWithProjectConcat() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //第一个{}
        Document username = new Document();
        username.put("username", "test");
        Document match = new Document();
        match.put("$match", username);
        //第二个{}
        Document unwind = new Document();
        unwind.put("$unwind", "$tags");

        //第三个{}
        Document concatStr = new Document();
        concatStr.put("$concat", Arrays.asList("$username", "-", "$tags"));
        Document field = new Document();
        field.put("_id", 0);
        field.put("Username_Tags", concatStr);

        Document project = new Document();
        project.put("$project", field);


        List<Document> list = new ArrayList<>();
        list.add(match);
        list.add(unwind);
        list.add(project);
        AggregateIterable<Document> documents = collection.aggregate(list);
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("Username_Tags"));
        }
    }

    /**
     * db.java_coll.aggregate([{$match:{username:'test'}},{$unwind:'$tags'},{$project:{_id:0,tags:'$tags',username:'$username'}}])
     */
    public void selectDocumentWithProject() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //第一个{}
        Document username = new Document();
        username.put("username", "test");
        Document match = new Document();
        match.put("$match", username);
        //第二个{}
        Document unwind = new Document();
        unwind.put("$unwind", "$tags");

        //第三个{}
        Document field = new Document();
        field.put("_id", 0);
        field.put("tags", "$tags");
        field.put("username", "$username");
        Document project = new Document();
        project.put("$project", field);


        List<Document> list = new ArrayList<>();
        list.add(match);
        list.add(unwind);
        list.add(project);
        AggregateIterable<Document> documents = collection.aggregate(list);
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("tags") + "\t" + doc.get("username"));
        }
    }
}
